package com.cqw.utils;

import com.cqw.common.HttpStatus;
import com.cqw.domain.SysUser;
import com.cqw.exception.CommonException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-07-18 17:24
 * @Description: 安全服务工具类
 */
public class SecurityUtils {


    /**
     * 获取用户
     *
     * @return
     */
    public static SysUser getLoginUser() {
        try {
            return (SysUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new CommonException(HttpStatus.UNAUTHORIZED, "获取用户信息异常");
        }
    }


    /**
     * 获取Authentication
     *
     * @return
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}
